<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%><%@taglib
	uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ page
	language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Share With Me</title>
<meta charset="utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 6]>
		<link href="css/ie6.css" rel="stylesheet" type="text/css" />
	<![endif]-->
<!--[if IE 7]>
        <link href="css/ie7.css" rel="stylesheet" type="text/css" />  
	<![endif]-->
<style type="text/css">
.happy-button {
	background-image:
		url('http://localhost:6467/ShareWithMe/images/bookworm.jpg');
	height: 100px;
	width: 100px;
}

input.button_add {
	background-image: url('/images/bookworm.jpg'); /* 16px x 16px */
	background-color: transparent; /* make the button transparent */
	background-repeat: no-repeat;
	/* make the background image appear only once */
	background-position: 0px 0px; /* equivalent to 'top left' */
	border: none; /* assuming we don't want any borders */
	cursor: pointer;
	/* make the cursor like hovering over an <a> element */
	height: 100%; /* make this the size of your image */
	width: 100%;
	padding-left: 16px; /* make text start to the right of the image */
	vertical-align: middle; /* align the text vertically centered */
}
</style>

<script type="text/javascript">
function submitForm(clickValue){
	var myTextBox = document.getElementById('ShareID');
	myTextBox.value=clickValue;
	formComment.submit();
}
</script>

</head>

<body>
	<div id="background">
		<div id="page">

			<div class="header">
				<div class="footer">
					<div class="body">

						<!-- include Menu -->
						<jsp:include page="Menu.jsp" flush="True">
							<jsp:param name="page_call" value="ShareWithMe" />
						</jsp:include>

						<form action="CommentServlet" method="post" name="formComment">
						
						<%-- ******** view process ********  --%>
						<sql:query dataSource="jdbc/sharewithme" var="rs">
						select count(*) as isbn from sharewithme.tbtopview where isbn = '${param.bisbn }'
						</sql:query>
						<c:forEach items="${rs.rows }" var="view">
						
							<c:choose>
								<c:when test="${view.isbn == 0}">
									
									<sql:update var="rs" dataSource="jdbc/sharewithme">
									insert into sharewithme.tbtopview(isbn, countview) values ('${param.bisbn }',1)
									</sql:update>
								</c:when>
								<c:otherwise>
									
									<sql:update var="rs" dataSource="jdbc/sharewithme">
									UPDATE ShareWithMe.TBTopView SET CountView= CountView + 1
									where ISBN = '${param.bisbn }'
									</sql:update>
								</c:otherwise>
							</c:choose>
						</c:forEach>

						<%-- ******** view process ********  --%>

							<input type="hidden" name="WormButton" id="ShareID" />
							<table border="1" width="70%">
								<tr>
									<th><input type="hidden" name="page_call"
										value="ShareWithMe">
										<table border="2" width="100%" bgcolor="grey">
											<tr>
												<sql:query dataSource="jdbc/sharewithme" var="rs">
												select bisbn, bnamethai, bnameeng, bwriter, bcountpage,
												bpublishing, bedition,btranslate, bprice 
												FROM  sharewithme.tbbooks
												where bisbn = '${param.bisbn }'
											</sql:query>
												<c:forEach items="${rs.rows }" var="book">
													<th><img height="200" width="150"></th>
													<th><h3>ชื่อหนังสือ : ${book.bnamethai }</h3> <input
														name="isbn" value="${param.bisbn }" type="hidden">
														คะแนน :  <jsp:include page="GiveAVote.jsp"/> <br> 
														จำนวนหน้า : ${book.bcountpage }<br>
														ราคา : ${book.bprice }<br> ผู้แต่ง : ${book.bwriter }<br>
														ผู้แปล : ${book.bpublishing }<br> <a
														href="BookDetail.jsp?bisbn=${book.bisbn }">ดูรายละเอียดเพิ่มเติม</a>
													</th>
												</c:forEach>
											</tr>
										</table></th>
								</tr>
								<tr>
									<th>
										<table border="2" width="100%" bgcolor="grey">
											<tr>
												<th>
													<h3>Hot Comment</h3>

												</th>

											</tr>
											<tr>
												<th>good comment <sql:query var="rs"
														dataSource="jdbc/sharewithme">
														select share.* from sharewithme.tbshare share,(
												select max(summary.hotcomment), summary.sharevalue, summary.worm from (
												select count(*) as hotcomment ,tbshare.sharevalue as sharevalue, allcomment.sharevalue as worm from 
												(select no, sharevalue
												FROM sharewithme.tbshare
												where sharegroupid = '5' and sharetypeid = 2
												 and isbn = '${param.bisbn }') allcomment, sharewithme.tbshare tbshare
												 where allcomment.no = tbshare.sharevalue
												 and tbshare.sharetypeid = 1 and allcomment.sharevalue = 1
												 group by tbshare.sharevalue, allcomment.sharevalue
												 )summary 
												 where summary.worm = 1) last
												 where share.no = last.sharevalue
												</sql:query> <c:forEach items="${rs.rows }" var="comment">
														<tr>
															<th>${comment.no }:${comment.sharecomment } :
																${comment.displayname } : ${comment.sharedate } <c:choose>
																	<c:when test="${comment.sharevalue == 1}">
																		<image
																			src="<%=request.getContextPath()%>/images/happy_worm.png"
																			onclick="JavaScript:submitForm(${comment.no })"
																			type="image"
																			style="height: 100px; width: 100px;cursor: pointer;" />

																		<sql:query dataSource="jdbc/sharewithme" var="rs">
																select count(*) as countlike FROM sharewithme.tbshare where 
																sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																and sharevalue = ${comment.no }  and 
																isbn = '${comment.isbn }'
</sql:query>
																		<c:forEach items="${rs.rows }" var="countlike">countlike : ${countlike.countlike }</c:forEach>

																	</c:when>

																</c:choose>
															</th>
														</tr>
													</c:forEach>
												</th>
											</tr>

											<tr>
												<th>bad comment <sql:query var="rs"
														dataSource="jdbc/sharewithme">
														select share.* from sharewithme.tbshare share,(
												select max(summary.hotcomment), summary.sharevalue, summary.worm from (
												select count(*) as hotcomment ,tbshare.sharevalue as sharevalue, allcomment.sharevalue as worm from 
												(select no, sharevalue
												FROM sharewithme.tbshare
												where sharegroupid = '5' and sharetypeid = 2
												 and isbn = '${param.bisbn }') allcomment, sharewithme.tbshare tbshare
												 where allcomment.no = tbshare.sharevalue
												 and tbshare.sharetypeid = 1 and allcomment.sharevalue = 0
												 group by tbshare.sharevalue, allcomment.sharevalue )summary 
												 where summary.worm = 0 ) last
												 where share.no = last.sharevalue
												</sql:query> <c:forEach items="${rs.rows }" var="comment">
														<tr>
															<th>${comment.no }:${comment.sharecomment } :
																${comment.displayname } : ${comment.sharedate } <c:choose>

																	<c:when test="${comment.sharevalue ==0}">
																		<image
																			src="<%=request.getContextPath()%>/images/angry_worm.png"
																			onclick="JavaScript:submitForm(${comment.no })"
																			type="image"
																			style="height: 100px; width: 100px;cursor: pointer;" />
																		<sql:query dataSource="jdbc/sharewithme" var="rs">
																select count(*) as countlike FROM sharewithme.tbshare where 
																sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																and sharevalue = ${comment.no }  and 
																isbn = '${comment.isbn }'
</sql:query>
																		<c:forEach items="${rs.rows }" var="countlike">countlike : ${countlike.countlike }</c:forEach>
																	</c:when>
																</c:choose>
															</th>
														</tr>
													</c:forEach></th>
											</tr>
										</table>
									</th>
								</tr>

								<tr>
									<th>
										<table border="2" width="100%" bgcolor="grey">
											<tr>
												<th>
													<h3>Comment</h3>
												</th>
											</tr>
											<sql:query dataSource="jdbc/sharewithme" var="rs">
											SELECT no, sharegroupid, sharetypeid, sharevalue, sharecomment, 
											tbuser.userid as userid, tbuser.displayname as displayname, isbn, DATE_FORMAT(sharedate, '%d/%m/%Y %H:%i') as sharedate 
											FROM sharewithme.tbshare tbshare
											, sharewithme.tbuser tbuser
											where sharegroupid = 5
											and sharetypeid = 2 and isbn = ${param.bisbn } 
											and tbshare.userid = tbuser.userid
											order by sharedate</sql:query>
											<c:forEach items="${rs.rows }" var="comment">
												<tr>
													<th>${comment.no }:${comment.sharecomment } :
														${comment.displayname } : ${comment.sharedate } <c:choose>
															<c:when test="${comment.sharevalue == 1}">


																<image
																	src="<%=request.getContextPath()%>/images/happy_worm.png"
																	onclick="JavaScript:submitForm(${comment.no })"
																	type="image"
																	style="height: 100px; width: 100px;cursor: pointer;" />
																<sql:query dataSource="jdbc/sharewithme" var="rs">
																select count(*) as countlike FROM sharewithme.tbshare where 
																sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																and sharevalue = ${comment.no }  and 
																isbn = '${comment.isbn }'
</sql:query>
																<c:forEach items="${rs.rows }" var="countlike">countlike : ${countlike.countlike }</c:forEach>
																<%-- check for disable button
																<sql:query var="rs" dataSource="jdbc/sharewithme"> 
																select no FROM sharewithme.tbshare where 
																sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1 
																and sharevalue = ${comment.no } and userid = '${comment.userid }'
																and isbn = '${comment.isbn }'
																</sql:query>
																<c:forEach items="${rs.rows }" var="isAllow">
																	<c:choose>
																		<c:when test="${isAllow.no == null}">
																			<input type="submit"
																				class="sprite happy-button button" name="WormButton"
																				value="${comment.no }" />
																		</c:when>
																		<c:otherwise>
																		<input type="submit"
																			class="sprite happy-button button" name="WormButton"
																			value="${comment.no }"  />
																		</c:otherwise>
																	</c:choose>

																</c:forEach>
																 --%>
															</c:when>
															<c:when test="${comment.sharevalue ==0}">
																<image
																	src="<%=request.getContextPath()%>/images/angry_worm.png"
																	onclick="JavaScript:submitForm(${comment.no })"
																	type="image"
																	style="height: 100px; width: 100px;cursor: pointer;" />

																<sql:query dataSource="jdbc/sharewithme" var="rs">
																select count(*) as countlike FROM sharewithme.tbshare where 
																sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																and sharevalue = ${comment.no }  and 
																isbn = '${comment.isbn }'
</sql:query>
																<c:forEach items="${rs.rows }" var="countlike">countlike : ${countlike.countlike }</c:forEach>
															</c:when>
														</c:choose>
													</th>
												</tr>
											</c:forEach>
										</table>
									</th>
								</tr>

								<tr>
									<th><c:choose>
											<c:when test="${sessionScope.User.getDisplayName() != null}">

												<table border="2" width="100%" bgcolor="grey">
													<tr>
														<th>
															<h3>Write Comment</h3> <textarea class="text" cols="50"
																name="comment" rows="10"></textarea> <br> By <input
															value="${sessionScope.User.getDisplayName() }"
															name="userId"><br> <input type="radio"
															name="worm" checked="checked" value="1"><img
															src="<%=request.getContextPath()%>/images/happy_worm.png"
															type="image" style="height: 100px; width: 100px;">
															<input type="radio" name="worm" value="0"><img
															src="<%=request.getContextPath()%>/images/angry_worm.png"
															type="image" style="height: 100px; width: 100px;">
															<br> <input type="submit" value="Share With You"
															name="shareButton">
														</th>
													</tr>
												</table>
											</c:when>
											<c:otherwise>Please <a href="Index.jsp">Login</a> to Comment Login</c:otherwise>
										</c:choose></th>
								</tr>
							</table>
						</form>
					</div>
				</div>
				<div class="shadow">&nbsp;</div>
			</div>
		</div>
	</div>
</body>
</html>